﻿using GetSmartIDEList.Core.Ulities;
using GetSmartIDEList.Lib.Common;
using GetSmartIDEList.Lib.ORM;
using GetSmartIDEList.Models;
using GetSmartIDEList.WebAPI.Models;
using Newtonsoft.Json;

namespace GetSmartIDEList.WebAPI.Common
{
    public static class ReposFunction
    {
        private static MySQLHelper mySQL = new MySQLHelper();

        private static string conStr = AppDBConfig.GetConnectionString();
        private static string reposUrl = AppConfigurtaionServices.Configuration.GetSection("ReposInf").GetSection("getReposUrl").Value.ToString();

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<resultData> GetAllRepos(string id)
        {
            //获取个人
            //string allRepos = HttpGet.Get($"https://gitee.com/api/v5/users/{id}/repos");

            //获取组织
            string allRepos = HttpGet.Get(string.Format(reposUrl, id), GetAPIAuthor.GetToken());

            if (allRepos != null)
            {
                List<repos> reposLst = JsonConvert.DeserializeObject<List<repos>>(allRepos);

                if (reposLst == null || reposLst.Count == 0)
                {
                    List<resultData> empty = new List<resultData>();
                    empty.Add(new resultData() { id = -1, name = "无数据" });

                    return empty;
                }
                else
                {
                    //先删除
                    bool isFlag = DeleteAllData();

                    List<resultData> resultAll = new List<resultData>();

                    if (isFlag)
                    {
                        //准备插入数据
                        InstallDBTable<tableLeft> leftDB = new InstallDBTable<tableLeft>();

                        foreach (var item in reposLst)
                        {
                            tableLeft left = new tableLeft();
                            left.name = item.name;

                            string cmdSQL = leftDB.InsertDate(left);
                            Console.WriteLine(cmdSQL);

                            //插入
                            int insert = mySQL.ExecuteNonQuery(conStr, cmdSQL);

                            if (insert > 0)
                            {
                                //成功：加入返回值
                                resultAll.Add(new resultData { id = item.id, name = item.name });
                            }
                        }
                    }

                    return resultAll;
                }
            }
            else
            {
                List<resultData> empty = new List<resultData>();
                empty.Add(new resultData() { id = -1, name = "无数据" });

                return empty;
            }
        }

        /// <summary>
        /// 删除所有数据
        /// </summary>
        /// <returns></returns>
        public static bool DeleteAllData()
        {
            string strSQL = SQLXMLHelper.data.DeleteAllRepos;
            int rows = mySQL.ExecuteNonQuery(conStr, strSQL);
            if (rows >= 0)
            {
                mySQL.ExecuteNonQuery(conStr, SQLXMLHelper.data.DeleteFavorRepos);
            }

            return true;
        }

        /// <summary>
        /// 移动到收藏
        /// </summary>
        /// <returns></returns>
        public static bool MoveData(string name)
        {
            InstallDBTable<tableRight> rightDB = new InstallDBTable<tableRight>();

            tableRight right = new tableRight()
            {
                name = name
            };

            string cmdSQL = rightDB.InsertDate(right);
            Console.WriteLine(cmdSQL);

            string strSQL = SQLXMLHelper.data.DeleteAllRepos + $" and name='{name}'";

            //先执行删除，如果删除成功，再移动到收藏
            int rows = mySQL.ExecuteNonQuery(conStr, strSQL);
            if (rows >= 0)
            {
                string[] vs = new string[2];
                vs[0] = SQLXMLHelper.data.DeleteFavorRepos + $" and name='{name}'";
                vs[1] = cmdSQL;


                //执行事务，插入右表并删除左表
                return mySQL.ExecuteTrans(conStr, vs);
            }
            else return true;
        }

        /// <summary>
        /// 取消收藏
        /// </summary>
        /// <returns></returns>
        public static bool MoveBack(string name)
        {
            InstallDBTable<tableLeft> rightDB = new InstallDBTable<tableLeft>();

            tableLeft left = new tableLeft()
            {
                name = name
            };

            string cmdSQL = rightDB.InsertDate(left);
            Console.WriteLine(cmdSQL);

            //先删除，如果删除完成，再插入左表
            string strSQL = SQLXMLHelper.data.DeleteFavorRepos + $" and name='{name}'";
            int rows = mySQL.ExecuteNonQuery(conStr, strSQL);
            if (rows >= 0)
            {
                string[] vs = new string[2];
                vs[0] = SQLXMLHelper.data.DeleteAllRepos + $" and name='{name}'";
                vs[1] = cmdSQL;

                //执行事务，插入左表并删除右表
                return mySQL.ExecuteTrans(conStr, vs);
            }
            else
            {
                return true;
            }
        }
    }
}
